package com.demo.mutilDataSource.impl;

import com.demo.api.user.service.MDSLimitService;
import com.demo.common.enums.ResultCode;
import com.demo.common.response.Result;
import com.demo.mutilDataSource.domain.TestLimit;
import com.demo.mutilDataSource.domain.TestUser;
import com.demo.mutilDataSource.service.TestLimitService;
import com.demo.mutilDataSource.service.TestUserService;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @author
 * @since
 */
@Service(version = "1.0.0", interfaceClass = MDSLimitService.class, timeout = 120000, retries = 3)
@Slf4j
public class MDSLimitServiceImpl implements MDSLimitService {

  @Autowired
  private TestUserService testUserService;

  @Autowired
  private TestLimitService testLimitService;

  private Lock lock = new ReentrantLock();

  @Override
  @GlobalTransactional
  public Result addUserAndLimitCommit() {
    log.info("seata分布式事务Id:{}", RootContext.getXID());
    boolean isError = false;
    lock.lock();
    try {
      log.info("新增用户开始");
      TestUser testUser = new TestUser();
      testUser.setIdno("seata mutil ds 01");
      testUser.setFullName("seata name ");
      testUser.setSex("man");
      testUserService.save(testUser);
      log.info("新增用户结束");
      log.info("新增额度开始");
      TestLimit testLimit = new TestLimit();
      testLimit.setDt(new Date());
      testLimit.setIdno("seata mutil ds 01");
      testLimit.setLimitAmount(new BigDecimal(11.22));
      testLimitService.save(testLimit);
      log.info("新增额度结束");
    } catch (Exception e){
      log.info("载入事务{}进行回滚" + e.getMessage(), RootContext.getXID());
      try {
        GlobalTransactionContext.reload(RootContext.getXID()).rollback();
      } catch (TransactionException e1) {
        log.info("GlobalTransactionContext reload异常  {}" + e.getMessage(), RootContext.getXID());
      }
      isError = true;
    } finally {
      lock.unlock();
    }

    if (isError) {
      return Result.fail(ResultCode.SYSTEM_ERROR);
    } else {
      return Result.suc(true);
    }

  }

  @Override
  @GlobalTransactional
  public Result addUserAndLimitRollback() {
    log.info("addUserAndLimitRollback 被调用");
    log.info("seata分布式事务Id:{}", RootContext.getXID());
    boolean isError = false;
    lock.lock();
    try {
      log.info("新增用户开始");
      TestUser testUser = new TestUser();
      testUser.setIdno("seata mutil ds 01");
      testUser.setFullName("seata name ");
      testUser.setSex("man");
      testUserService.save(testUser);
      log.info("新增用户结束");
      log.info("新增额度开始");
      TestLimit testLimit = new TestLimit();
      testLimit.setDt(new Date());
      testLimit.setIdno("seata mutil ds 01");
      testLimit.setLimitAmount(new BigDecimal(11.22));
      testLimitService.save(testLimit);
      log.info("新增额度结束");
      int i = 1 / 0;
    } catch (Exception e){
      log.info("载入事务{}进行回滚" + e.getMessage(), RootContext.getXID());
      try {
        GlobalTransactionContext.reload(RootContext.getXID()).rollback();
      } catch (TransactionException e1) {
        log.info("GlobalTransactionContext reload异常  {}" + e.getMessage(), RootContext.getXID());
      }
      isError = true;
    } finally {
      lock.unlock();
    }

    if (isError) {
      return Result.fail(ResultCode.SYSTEM_ERROR);
    } else {
      return Result.suc(true);
    }
  }
}


